package com.activities;


import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.Adapters.TabsPagerAdapter;
import com.Animations.AnimUtil;
import com.Data.User.onUpdateStateCompleted;
import com.Dialogs.DialogShareText;
import com.Init.SliderClass;
import com.Init.ImageDownloaderInit;
import com.Listeners.MainTabSwiperClass;
import com.Managers.FragmentManagerClass;
import com.Managers.HomeManagerClass;
import com.Managers.PictureIntentManagerClass;
import com.Managers.UserManagerClass;
import com.TextFormat.UtilFonts;
import com.gcm.PlayServices;
import com.shareup.R;
import com.utils.AppConstant;
import com.utils.PagerUtil;


public class ActivityMain extends FragmentActivity {

    public static HomeManagerClass hmc;

    public static Handler mHandlerr;

    public static boolean sMenuOpened = false;

    public View containerMine;

    public TextView title;

    private ImageView homeImage;

    private RelativeLayout notificationContainer;

    private TextView notificationText;

    private TabsPagerAdapter pagerAdapter;

    private PagerTabStrip pagerTab;

    private final PlayServices pServices = new PlayServices();

    private String regid;

    private ImageView shareImage;

    private ViewPager vpager;

    public RelativeLayout getNotificationContainer() {

        return this.notificationContainer;
    }

    public TextView getNotificationText() {

        return this.notificationText;
    }

    public void Init() {

        mHandlerr = new Handler(Looper.getMainLooper()) {

            @Override
            public void handleMessage(Message inputMessage) {

                int type = inputMessage.getData().getInt("TYPE");
                int id = inputMessage.getData().getInt("ID");
                int count = inputMessage.getData().getInt("COUNT");
                //
                if (type == 0) {
                    FragmentManagerClass.aExplore.updateLikes(id, count);
                }
                else
                    if (type == 1) {
                        FragmentManagerClass.aExplore.updateComments(id, count);
                    }
            }
        };

        FragmentManagerClass.aMain = this;

        // NOTIFICATION ICON
        notificationContainer = (RelativeLayout) findViewById(R.id.container_notifications);
        notificationText = (TextView) findViewById(R.id.notification_text);
        notificationContainer.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                homeImage.performClick();

            }
        });
        notificationText.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                homeImage.performClick();

            }
        });
        notificationContainer.setVisibility(View.INVISIBLE);
        // SET CONTROLS
        vpager = (ViewPager) findViewById(R.id.mPager);
        pagerTab = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
        pagerTab.setTabIndicatorColorResource(R.color.main_selected);
        vpager.setOnPageChangeListener(new MainTabSwiperClass(this));
        pagerAdapter = new TabsPagerAdapter();
        vpager.setAdapter(pagerAdapter);
        for (int i = 0; i < pagerTab.getChildCount(); ++i) {
            View nextChild = pagerTab.getChildAt(i);
            if (nextChild instanceof TextView) {
                TextView tt = (TextView) nextChild;
                tt.setGravity(Gravity.CENTER_VERTICAL);
                UtilFonts.setTextBoldFont(tt);
            }
        }
        new PagerUtil(pagerAdapter, vpager);
        // CREATING HOME MANAGER
        hmc = new HomeManagerClass(vpager, pagerAdapter, this, pagerTab);

        // CREATE THE MENU SLIDER
        new SliderClass(this).Create();
        // SETTING TITLE
        title = (TextView) findViewById(R.id.title_actionbar_main);
        title.setText(getTitle());
        title.setTextSize(48);
        Typeface tf = Typeface.createFromAsset(getAssets(), "font/honey.ttf");
        title.setTypeface(tf);
        //
       
        shareImage = (ImageView) findViewById(R.id._share);
        shareImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                createChooser();

            }
        });
     
        homeImage = (ImageView) findViewById(R.id.show_menu);
        homeImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                FragmentManagerClass.sMenu.toggle(true);

            }
        });

        
   
        hmc.changeViewMenu(PagerUtil.FRAGMENT_NEWS);
     

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == PictureIntentManagerClass.START_UPLOADING) {
            if (resultCode == RESULT_OK) {
                FragmentManagerClass.RefreshPictureList();
            }
        }
        else
            if (requestCode == ActivityUpload.SELECT_PHOTO) {
                if (resultCode == RESULT_OK) {
                    PictureIntentManagerClass.startIntentForCrop(false, data, this);
                }
            }
            else
                if (requestCode == ActivityUpload.CROP_PIC) {
                    if (resultCode == RESULT_OK) {
                        Intent intent = new Intent(this, ActivityUpload.class);
                        startActivityForResult(intent, PictureIntentManagerClass.START_UPLOADING);
                        AnimUtil.getActivityTransition(this, true);
                    }
                }
                else
                    if (requestCode == ActivityProfile.CROPED_PICTURE_PROFILE) {
                        if (resultCode == RESULT_OK) {

                            PictureIntentManagerClass.uploadPicture(this, true, false, null, null);
                            FragmentManagerClass.aMenu.updateUserInformation(true);
                        }

                    }
                    else
                        if (requestCode == ActivityProfile.SEND_TO_COVER) {
                            if (resultCode == RESULT_OK) {
                                Uri path = PictureIntentManagerClass.getImageUri(data, false);
                                PictureIntentManagerClass.uploadPicture(this, false, true, path, null);
                            }
                        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (sMenuOpened) {
                FragmentManagerClass.sMenu.toggle();
                sMenuOpened = false;
            }
            else {
                moveTaskToBack(true);
                UserManagerClass.updateState(0, new onUpdateStateCompleted() {

                    @Override
                    public void Completed(String result) {

                        hmc.destroy();
                        finish();

                    }
                });

            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_MENU) {
            FragmentManagerClass.sMenu.toggle(true);
            return true;
        }

        return super.onKeyUp(keyCode, event);
    }

  

    private void createChooser() {

        DialogShareText td = new DialogShareText(this);
        td.show();
    }

   

    private void PlayServicesStuff() {

        // Obtenemos el Registration ID guardado
        regid = pServices.getRegistrationId(this);
        // Si no disponemos de Registration ID comenzamos el registro
        //
        if (UserManagerClass.UserLogged != null) {
            pServices.registrar(String.valueOf(UserManagerClass.UserLogged.getID()), this);
            //
            if (regid.equals("")) {
                pServices.registrar(String.valueOf(UserManagerClass.UserLogged.getID()), this);
            }
        }
        Init();

    }

   
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.setProperty("http.keepAlive", "false");
        ImageDownloaderInit.config(this, "");
        PlayServicesStuff();

    }

    // start region SET PROFILE AND COVER PICTURE

    @Override
    protected void onPause() {
    	
        super.onPause();
    }

    @Override
    protected void onResume() {

        if (this.vpager == null)
            Init();

        super.onResume();
    }

    // end region
    
   
}
